use sea_orm_migration::prelude::*;

use crate::idents::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(MessageQueue::Table)
                    .col(ColumnDef::new(MessageQueue::QueueId).binary().primary_key())
                    .col(ColumnDef::new(MessageQueue::Name).text().not_null())
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(DeviceQueueSub::Table)
                    .col(
                        ColumnDef::new(DeviceQueueSub::Id)
                            .big_integer()
                            .primary_key()
                            .auto_increment(),
                    )
                    .col(ColumnDef::new(DeviceQueueSub::Name).text().not_null())
                    .col(ColumnDef::new(DeviceQueueSub::DeviceId).binary().not_null())
                    .col(ColumnDef::new(DeviceQueueSub::QueueId).binary().not_null())
                    .col(ColumnDef::new(DeviceQueueSub::ReaderId).binary().not_null())
                    .foreign_key(
                        ForeignKey::create()
                            .name("FK_device_queue_sub_on_queue_id_to_message_queue")
                            .from(DeviceQueueSub::Table, DeviceQueueSub::QueueId)
                            .to(MessageQueue::Table, MessageQueue::QueueId),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_message_queue_on_name")
                    .table(DeviceQueueSub::Table)
                    .col(DeviceQueueSub::Name)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_device_queue_sub_on_name")
                    .table(DeviceQueueSub::Table)
                    .col(DeviceQueueSub::Name)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_device_queue_sub_on_device_id")
                    .table(DeviceQueueSub::Table)
                    .col(DeviceQueueSub::DeviceId)
                    .to_owned(),
            )
            .await?;

        manager
            .create_index(
                Index::create()
                    .name("IDX_device_queue_sub_on_queue_id")
                    .table(DeviceQueueSub::Table)
                    .col(DeviceQueueSub::QueueId)
                    .to_owned(),
            )
            .await?;

        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(DeviceQueueSub::Table).to_owned())
            .await?;

        manager
            .drop_table(Table::drop().table(MessageQueue::Table).to_owned())
            .await?;

        Ok(())
    }
}
